package com.tedu.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * 这是异常信息收集的切面
 */
@Component
@Aspect   //定义一个切面   <aop:aspect ref="firstAspect">
public class ExceptionAspect {

    /**
     * 定义通知  当业务方法执行出现异常时，进行该方法    切入点
     */
    @AfterThrowing(value = "within(com.tedu.service.*)",throwing = "throwable")
    public void afterThrowing(JoinPoint joinPoint,Throwable throwable){
        //通过joinPoint 获取目标对象
        Object target = joinPoint.getTarget();
        //通过joinPoint获取执行方法信息
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //记录异常信息
        String exceptioninfo = "在访问"+target+"的"+signature.getName()+"出现了异常；异常信息是："+throwable.getMessage();
        System.out.println(exceptioninfo);

    }

}
